$MOD186
Name Timer2

;; Serial routines for 8088 microcomputer system
;
; Filename:	Timer.ASM
;
; Author: 	Law Wen Yong
;
; This file contains proprietory information and cannot be copied 
; or distributed without prior permission from the author.
;---------------------------------------------------------------------------

;API:
public	start_timer2  ;activate the timer2
public  TIMER2_INTR ;timer 2 ISR
;in main.asm
extrn 	timer2_action:far

$include(80188.inc)

Timer2_Routine SEGMENT
ASSUME CS:Timer2_Routine


Start_timer2      proc Far
	push ax
	push dx
	;Initialize Timer2	
	;initialize the current count value to 0
	;Timer count will be incremented every forth CPU-cycle
	mov ax, 0;
	mov dx, T2_CNT;
	OUT DX, AL
	
	;set the max count to 60000. That will be like 33Hz interrupt. That will be timer interrupt 0.03 seconds
	MOV AX, 60000;
	MOV DX, T2_CA;
	OUT DX, AL

	;start timer count, enable timer 2 interrupt, enable the timer to count continuously by setting the last bit to 1
	MOV AX,0E001H 
	MOV DX, T2_CON
	OUT DX, AL

	;set timer interrupt as 1 level priority level which is the second highest level, set it to non-maskable
	MOV DX, TIMER_CTRL
	MOV AL, 01H
	OUT DX, AL
	pop dx
	pop ax

ret
Start_timer2 endp


; **************** Start of TIMER2_INTR service routine ******************
TIMER2_INTR     proc Far
		PUSH   AX
		PUSH   DX
; Action to be taken on timer2 interrupt to be written by user
		CALL	FAR PTR TIMER2_ACTION
              
				;return interrupt
                ;RESET_INT_CTL
                MOV DX, EOI
                MOV AX, 8
                OUT DX, AL
				
		POP DX
		POP	AX
		
		IRET
; **************** End of TIMER2_INTR service routine **********************
TIMER2_INTR endp


Timer2_Routine ENDS

End
